package zookeeper.curator.testcurator1;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * Reference https://blog.csdn.net/wo541075754/article/details/69138878
 *
 * Created by majian8 on 2018/6/18.
 */
public class ClientUtils {
    public static CuratorFramework connect() {
        //curator 有三种创建方式 前两种的底层都是用第三种方式实现的
        //第一种
       /* RetryPolicy retryPolicy1 = new ExponentialBackoffRetry(1000,3);
        CuratorFramework client1 = CuratorFrameworkFactory.newClient("192.168.209.128:2181",retryPolicy1);
        client1.start();*/

        //第二种

       /* RetryPolicy retryPolicy2 = new ExponentialBackoffRetry(1000,3);
        CuratorFramework client2 = CuratorFrameworkFactory.newClient("192.168.209.128:2181",
                5000,1000,retryPolicy2);
        client2.start();*/
        //第三种
        RetryPolicy policy=new ExponentialBackoffRetry(1000,3);//重试接口
        CuratorFramework client= CuratorFrameworkFactory.builder()
                .connectString("192.168.209.129:2181")
                .retryPolicy(policy)
                .sessionTimeoutMs(6000)
                .connectionTimeoutMs(3000)
                .namespace("demo")//这个demo 可能表示顶级节点
                .build();
        client.start();

        return client;

    }
}
